<style>
    /*#purchasePlan-edit .layui-table-body {*/
    /*    margin-right: -17px;*/
    /*}*/
    .layui-layer-title {
        display: flex;
        align-items: center;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">申报部门：</label>
                                    <input name="queryRoleId" type="hidden"/>
                                    <div class="layui-input-inline mr0">
                                        <div id="selectQueryRole"></div>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline mr0">
                                        <select name="status">
                                            <option value="">请选择状态</option>
                                            <option value="stayConfirm">待确认</option>
                                            <option value="stayAudit">待审核</option>
                                            <option value="success">审核驳回</option>
                                            <option value="fail">已审核</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">计划单号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="planNo" class="layui-input" type="text"
                                               placeholder="请输入计划单号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">时间段：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" type="text"
                                               placeholder="选择时间" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit layui-form-keyDownSearch="true">查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="purchasePlanBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="purchasePlanTable" lay-filter="purchasePlanTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchasePlanTableBar">
    {{# if(d.status == 'success'){}}
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="info">详情</a>
    {{# } }}
    {{# if((d.status == 'stayConfirm' || d.status == 'fail')){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    {{# } }}
    {{# if(d.status == 'stayConfirm'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="confirm">确认</a>
    {{# } }}
    {{# if(d.status == 'stayAudit'){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="audit">审核</a>
    {{# } }}
    {{# if(d.status != 'success'){}}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>

<script type="text/html" id="purchasePlanInfo">
    <form id="purchasePlan-form" lay-filter="purchasePlan-form" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div class="layui-row">
                    <div class="layui-col-xs6">
                        <label class="layui-form-label">申报部门</label>
                        <div class="layui-input-block">
                            <select name="departmentId" lay-verify="required"></select>
                        </div>
                    </div>

                    <div class="layui-col-xs6">
                        <label class="layui-form-label">紧急程度</label>
                        <div class="layui-input-block">
                            <select name="purchaseUrgencyId" lay-verify="required" disabled></select>
                        </div>
                    </div>
                </div>
                <table class="layui-table" id="purchasePlanInfo-table" lay-filter="purchasePlanInfo-table"></table>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            </div>
        </div>
    </form>
</script>

<style>
    #purchasePlan-form .layui-disabled {
        color: #393d49 !important;
    }
</style>

<script type="text/html" id="purchasePlanDepartmentList">
    <option value="">请选择部门</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.code}}-{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="purchasePlanForm">
    <form id="purchasePlan-edit" lay-filter="purchasePlan-edit" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto" style="padding-top: 10px;">
                <input name="id" type="hidden"/>
                <div class="layui-form-item">
                    <div class="layui-col-xs6">
                        <div id="purchasePlan-btn-div">
                            <div class="layui-input-block">
                                <button id="purchasePlan-btn-aduit" class="layui-btn icon-btn"><i
                                        class="layui-icon">&#xe60f;</i>领料计划审核
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-xs6">
                        <label class="layui-form-label">申报部门<span style="color:red">*</span></label>
                        <input name="roleId" type="hidden"/>
                        <div class="layui-input-block" id="selectRole">
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <label class="layui-form-label">紧急程度<span style="color:red">*</span></label>
                        <div class="layui-input-block">
                            <select name="purchaseUrgencyId" lay-verify="required"></select>
                        </div>
                    </div>
                </div>
                <table class="layui-table" id="purchasePlan-edit-table" lay-filter="purchasePlan-edit-table"></table>
            </div>
            <div class="layui-form-item text-right" id="planEdit-footer">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="purchasePlan-edit-submit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<script type="text/html" id="purchasePlanAdit">
    <form id="purchasePlan-aduit" lay-filter="purchasePlan-aduit" class="layui-form model-form">
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left:45px;">
                <textarea id="purchasePlan-reason" placeholder="请输入审核结果" maxlength="200"
                          class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <div class="layui-btn" id="purchasePlan-aduit-submit1">驳回</div>
            <div class="layui-btn" id="purchasePlan-aduit-submit2">通过</div>
        </div>
    </form>
</script>

<script type="text/html" id="planDepartmentListEdit">
    <option value="">请选择申报部门</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="erpInventoryListEdit">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <input id="erpInventoryListEdit-code" class="layui-input search-input" type="text"
                                   placeholder="输入物料编号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline mr0">
                            <input id="erpInventoryListEdit-name" class="layui-input search-input" type="text"
                                   placeholder="输入物料名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input id="addSelfCollectedGoodsProductCategoryId"
                                   name="addSelfCollectedGoodsProductCategoryId" type="hidden" lay-verify="required">
                            <div class="layui-input-inline mr0" id="selectProductType"></div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="erpInventoryListEdit-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="erpInventoryListEdit-btn-choose" class="layui-btn layui-btn-normal">确认选择
                        </button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="erpInventoryListEdit-table" lay-filter="erpInventoryListEdit-table"></table>
        </div>
    </div>
</script>

<script type="text/html" id="erpInventoryListErpInventoryClassIdEdit">
    <option value="">请选择物料类别</option>
    {{# layui.each(d, function(index, item){ }}
    {{#
    var space = "";
    if(item.grade > 0){
    for(var j = 0; j < item.grade; j++){
    space += "&nbsp;&nbsp;";
    }
    }
    var disabled = "";
    if(item.isDisable == 1 || item.isParent){
    disabled = "disabled";
    }
    }}
    <option value="{{item.id}}" {{disabled}}>{{space}}{{item.code}}-{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="purchasePlanBusinessTypeEdit">
    <option value="">请选择业务类型</option>
    {{# layui.each(d, function(index, item){ }}
    <option value="{{item.id}}">{{item.name}}</option>
    {{# }) }}
</script>

<script type="text/html" id="skuSpec">
    {{# var skuSpec = "";
    if(d.skuSpec != null){
    skuSpec = d.skuSpec;
    }
    }}
    <div><input name="skuSpec" placeholder="请输入规格型号" type="text" value="{{skuSpec}}"
                class="layui-input" maxlength="50" style="border: 0;height: 28px;text-align:center;"/></div>
</script>


<script type="text/html" id="purchaseNumberEdit">
    {{# var purchaseNumber = "";
    var isRequired = false;
    if(d.purchaseNumber != null){
    purchaseNumber = d.purchaseNumber;
    isRequired = true;
    }
    }}
    <div><input name="purchaseNumber" lay-filter="required" placeholder="请输入采购数量" type="number"
                value="{{purchaseNumber}}" lay-verify="{{isRequired ? 'required|quantity' : ''}}"
                required="{{isRequired ? true : false}}" class="layui-input" maxlength="10"
                style="border: 0;height: 28px;text-align:center;"/></div>
</script>


<script type="text/html" id="useAddressEdit">
    {{# var useAddress = "";
    if(d.useAddress != null){
    useAddress = d.useAddress;
    }
    }}
    <div><input name="useAddress" placeholder="请输入使用地址" type="text" value="{{useAddress}}" class="layui-input"
                maxlength="50" style="border: 0;height: 28px;text-align:center;"/></div>
</script>

<script type="text/html" id="lastChangeTimeEdit">
    {{# var lastChangeTime = "";
    if(d.lastChangeTime != null){
    lastChangeTime = d.lastChangeTime;
    }
    }}
    <div><input name="lastChangeTime" placeholder="请输入上次更新时间" type="text" value="{{lastChangeTime}}"
                class="layui-input" maxlength="50" style="border: 0;height: 28px;text-align:center;"/></div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="purchasePlan-edit-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'util', 'table', 'admin', 'laytpl', 'laydate', 'tableTreeDj'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var util = layui.util;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var tableTree = layui.tableTreeDj;

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("#sTime").val(dateValue[0] + " 00:00:00");
                    $("#eTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#sTime").val();
                    $("#eTime").val();
                }
            }
        });

        // 获取部门数据
        admin.req('user/admin/role/list', {limit: 9999}, function (res) {
            if (res.code == 200) {
                for (let i = 0; i < res.data.length; i++) {
                    res.data[i].children = [];
                }
                xmSelect.render({
                    el: '#selectQueryRole',
                    model: {label: {type: 'text'}},
                    radio: true,
                    clickClose: true,
                    filterable: true,
                    tree: {
                        show: true,
                        strict: false,
                        expandedKeys: true,
                    },
                    tips: '顶级部门',
                    height: 'auto',
                    prop: {
                        name: 'name',
                        value: 'id',
                        children: 'children'
                    },
                    data() {
                        return xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                    },
                    on: function (data) {
                        //被选中的select的id,如果没有 -1
                        let roleId = data.arr.length > 0 ? data.change[0].id : -1;
                        $("input[name='queryRoleId']").val(roleId);
                    },
                });
            } else {
                layer.msg('获取部门失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        // 渲染表格
        var insTb = table.render({
            elem: '#purchasePlanTable',
            url: config.base + 'enterprise/purchase/admin/purchasePlan/list',
            toolbar: true,
            defaultToolbar: ["filter"],
            where: {access_token: config.getToken().access_token},
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'departmentName', align: 'center', title: '申报部门', width: 150},
                {field: 'planNo', align: 'center', title: '计划单号', width: 200},
                {field: 'creatorName', align: 'center', title: '创建人'},
                {field: 'statusDesc', title: '状态', align: 'center'},
                {field: 'affirmantName', title: '确认人', align: 'center'},
                {field: 'auditorName', title: '审核人', align: 'center'},
                {field: 'checkStatusDesc', title: '核对状态', align: 'center'},
                {field: 'createTime', sort: false, title: '创建时间', width: 170},
                {align: 'center', toolbar: '#purchasePlanTableBar', title: '操作', width: 200}
            ]],

        });

        // 工具条点击事件
        table.on('tool(purchasePlanTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'confirm') { // 确认
                doConfirm(data);
            } else if (layEvent === 'audit') { // 审核
                // showEditModel(data, 1, null);
                // var data = $("#purchasePlan-edit").serializeObject();

                admin.req('enterprise/purchase/admin/purchasePlan/detail/'+data.id,{},(res)=>{
                    if(res.code===200){
                        openFormFun('audit',res.data)
                    }
                })
            } else if (obj.event === 'edit') { // 修改
                // showEditModel(data, 2, data.roleId);
                admin.req('enterprise/purchase/admin/purchasePlan/detail/'+data.id,{},(res)=>{
                    if(res.code===200){
                        openFormFun('edit',res.data)
                    }
                })


            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            } else if (obj.event === 'info') {  //明细
                admin.req('enterprise/purchase/admin/purchasePlan/detail/'+data.id,{},(res)=>{
                    if(res.code===200){
                        openFormFun('detail',res.data)
                    }
                })
                // showPlanInfo(obj)
            }
        });

        //监听排序
        table.on('sort(purchasePlanTable)', function (obj) {
            table.reload('purchasePlanTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            insTb.reload({where: admin.resetSearch(data.field), page: {curr: 1}}, 'data');
        });

        form.render('select')

        // 添加按钮点击事件
        $('#purchasePlanBtnAdd').click(function () {
            // showEditModel(null, null, null);
            openFormFun('add')
        });

        function openFormFun(eventName,data) {
            admin.formOpen({
                area: '90%',
                offset: '50px',
                title: '采购需求单审核',
                path: 'components/purchase/purchasePlanForm.html',
                success: function () {
                    parentData.eventName = eventName
                    parentData.data=data
                },

            })
        };
        //确认
        var doConfirm = function (data) {
            layer.confirm('要确认吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchasePlan/confirm', {id: data.id}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        table.reload('purchasePlanTable', {});
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                    }
                }, 'GET');
            });
        };

        // 显示编辑弹窗
        function showEditModel(data, type, roleId) {
            var title = '新增领料计划信息';
            if (data) {
                if (type == 1) {
                    title = '审核领料计划'
                } else {
                    title = '更新领料计划信息'
                }
            }
            admin.open({
                type: 1,
                area: ['90%', '93%'],
                title: title,
                content: $('#purchasePlanForm').html(),
                success: function () {
                    var dataList = [];
                    var erpPurchasePlanInfos = []; //明细数据
                    var usualData = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}];
                    var codeArr = [];//物料选择集合
                    var reason = '';
                    let arr2treeExt;

                    //自定义验证规则
                    form.verify({
                        quantity: [/^([1-9]\d*(\.\d{1,2})?|([0](\.([0][0-9][1-9]|[1-9]\d{0,1})))|[ \t]*)$/, '格式错误']
                    });

                    if (type == 1) { //审核
                        $("#planEdit-footer").hide();
                    } else {  //修改
                        $("#purchasePlan-btn-div").hide();
                    }

                    // 获取部门
                    admin.req('user/admin/role/list', {limit: 9999}, function (res) {
                        if (res.code == 200) {
                            for (let i = 0; i < res.data.length; i++) {
                                res.data[i].children = [];
                                if (roleId != undefined && roleId != null && res.data[i].id === roleId) {
                                    res.data[i].selected = true;
                                    $("input[name='roleId']").val(roleId);
                                } else {
                                    res.data[i].selected = false;
                                }
                            }
                            arr2treeExt = xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                            xmSelect.render({
                                el: '#selectRole',
                                model: {label: {type: 'text'}},
                                radio: true,
                                clickClose: true,
                                filterable: true,
                                tree: {
                                    show: true,
                                    strict: false,
                                    expandedKeys: true,
                                },
                                tips: '顶级角色',
                                height: 'auto',
                                prop: {
                                    name: 'name',
                                    value: 'id',
                                    children: 'children'
                                },
                                data() {
                                    return arr2treeExt;
                                },
                                on: function (data) {
                                    //被选中的select的id,如果没有 -1
                                    let roleId = data.arr.length > 0 ? data.change[0].id : -1;
                                    $("input[name='roleId']").val(roleId);
                                },
                            });
                        } else {
                            layer.msg('获取部门失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');


                    // 获取紧急程度
                    admin.req('enterprise/purchase/admin/purchaseUrgency/list', {limit: 9999}, function (res) {
                        if (res.code == 200) {
                            var html = '<option value="">请选择紧急程度</option>';
                            for (var k = 0; k < res.data.length; k++) {
                                html += '<option value="' + res.data[k].id + '">' + res.data[k].purchaseUrgencyName + '</option>';
                            }
                            $("form select[name='purchaseUrgencyId']").html(html);
                            form.render('select');
                            if (data && data.purchaseUrgencyId) {
                                form.val('purchasePlan-edit', data);
                            }
                        } else {
                            layer.msg('获取紧急程度失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');

                    if (data) {
                        var planId = data.id;
                        reason = data.reason;
                        admin.req('enterprise/purchase/admin/purchasePlan/getDetailInfo', {planId: planId}, function (data) {
                            layer.closeAll('loading');
                            if (data.code == 200) {
                                var detail = data.data;
                                for (var i = 0; i < detail.length; i++) {
                                    dataList.push({});
                                    dataList[i].id = detail[i].id;
                                    dataList[i].skuCode = detail[i].skuCode;
                                    dataList[i].productName = detail[i].productName;
                                    dataList[i].skuSpec = detail[i].skuSpec ? detail[i].skuSpec : "";
                                    dataList[i].mainUnitName = detail[i].mainUnitName;
                                    dataList[i].productTypeName = detail[i].productTypeName ? detail[i].productTypeName : "";
                                    dataList[i].purchaseNumber = detail[i].purchaseNumber ? detail[i].purchaseNumber : "";
                                    dataList[i].useAddress = detail[i].useAddress ? detail[i].useAddress : "";
                                    dataList[i].lastChangeTime = detail[i].lastChangeTime ? detail[i].lastChangeTime : "";
                                    erpPurchasePlanInfos.push({});
                                    erpPurchasePlanInfos[i].id = detail[i].id;
                                    erpPurchasePlanInfos[i].skuCode = detail[i].skuCode;
                                    erpPurchasePlanInfos[i].productName = detail[i].productName;
                                    erpPurchasePlanInfos[i].skuSpec = detail[i].skuSpec ? detail[i].skuSpec : "";
                                    erpPurchasePlanInfos[i].mainUnitName = detail[i].mainUnitName;
                                    erpPurchasePlanInfos[i].productTypeName = detail[i].productTypeName ? detail[i].productTypeName : "";
                                    erpPurchasePlanInfos[i].purchaseNumber = detail[i].purchaseNumber ? detail[i].purchaseNumber : "";
                                    erpPurchasePlanInfos[i].useAddress = detail[i].useAddress ? detail[i].useAddress : "";
                                    erpPurchasePlanInfos[i].lastChangeTime = detail[i].lastChangeTime ? detail[i].lastChangeTime : "";
                                    usualData.push({});
                                    usualData[i].purchaseNumber = detail[i].purchaseNumber ? detail[i].purchaseNumber : "";
                                    usualData[i].useAddress = detail[i].useAddress ? detail[i].useAddress : "";
                                    usualData[i].lastChangeTime = detail[i].lastChangeTime ? detail[i].lastChangeTime : "";
                                    codeArr.push(detail[i].skuCode);
                                }
                                dataList.push({}, {}, {}, {}, {}, {}, {}, {}, {}, {});
                                usualData.push({}, {}, {}, {}, {}, {}, {}, {}, {}, {});

                                // 渲染表格
                                table.render({
                                    elem: '#purchasePlan-edit-table',
                                    data: dataList,
                                    limit: dataList.length,
                                    cellMinWidth: 100,
                                    colGroup: true,
                                    // height: 450,
                                    cols: [[
                                        {type: 'numbers'},
                                        {
                                            field: 'productName',
                                            align: 'center',
                                            title: '物料名称',
                                            templet: '<div><span class="erpInventoryId" style="display: block;cursor: pointer;" data-add="{{d.productName != null ? false : true}}">{{d.productName != null ? d.productName : "请选择物料"}}</span></div>'
                                        },
                                        {field: 'skuCode', align: 'center', title: '物料编号'},
                                        {field: 'skuSpec', align: 'center', title: '规格型号'},
                                        {field: 'mainUnitName', align: 'center', title: '计量单位'},
                                        {field: 'productTypeName', align: 'center', title: '物料类别'},
                                        {
                                            title: "数量<span style='color:red;'>*</span>",
                                            align: 'center',
                                            templet: '#purchaseNumberEdit',
                                            width: '10%'
                                        },
                                        {title: '使用地址', align: 'center', templet: '#useAddressEdit'},
                                        {
                                            title: '上次更新时间',
                                            align: 'center',
                                            templet: '#lastChangeTimeEdit'
                                        },
                                        {
                                            align: 'center',
                                            toolbar: '#purchasePlan-edit-table-bar',
                                            title: '操作',
                                            width: 100
                                        }
                                    ]],
                                    done: function () {
                                        $("input[name='purchaseNumber']").on("input", function (e) {
                                            //获取input输入的值
                                            var curValue = e.delegateTarget.value;
                                            var curIndex = $(this).closest("tr").data("index");
                                            if (erpPurchasePlanInfos[curIndex]) {
                                                erpPurchasePlanInfos[curIndex].purchaseNumber = curValue;
                                            }
                                            var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                            var purchaseNumber = $(inputs[0]).val();    //数量
                                            var useAddress = $(inputs[1]).val();        //使用地址
                                            var lastChangeTime = $(inputs[2]).val();          //上次更换时间
                                            if (purchaseNumber == '' || purchaseNumber < 0) {
                                                purchaseNumber = 0;
                                                $(inputs[0]).val(0);
                                            }
                                            usualData[curIndex].purchaseNumber = purchaseNumber;
                                            usualData[curIndex].useAddress = useAddress;
                                            usualData[curIndex].lastChangeTime = lastChangeTime;
                                            if (usualData.length - curIndex - 1 < 10) {
                                                var addLength = 10 - (usualData.length - curIndex - 1);
                                                for (var i = 0; i < addLength; i++) {
                                                    usualData.push({});
                                                }
                                            }
                                        });
                                        $("input[name='useAddress']").on("input", function (e) {
                                            //获取input输入的值
                                            var curValue = e.delegateTarget.value;
                                            var curIndex = $(this).closest("tr").data("index");
                                            if (erpPurchasePlanInfos[curIndex]) {
                                                erpPurchasePlanInfos[curIndex].useAddress = curValue;
                                            }
                                            var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                            var purchaseNumber = $(inputs[0]).val();    //数量
                                            var useAddress = $(inputs[1]).val();        //使用地址
                                            var lastChangeTime = $(inputs[2]).val();          //上次更换时间
                                            usualData[curIndex].purchaseNumber = purchaseNumber;
                                            usualData[curIndex].useAddress = useAddress;
                                            usualData[curIndex].lastChangeTime = lastChangeTime;
                                            if (usualData.length - curIndex - 1 < 10) {
                                                var addLength = 10 - (usualData.length - curIndex - 1);
                                                for (var i = 0; i < addLength; i++) {
                                                    usualData.push({});
                                                }
                                            }
                                        });
                                        $("input[name='lastChangeTime']").on("input", function (e) {
                                            //获取input输入的值
                                            var curValue = e.delegateTarget.value;
                                            var curIndex = $(this).closest("tr").data("index");
                                            if (erpPurchasePlanInfos[curIndex]) {
                                                erpPurchasePlanInfos[curIndex].lastChangeTime = curValue;
                                            }
                                            var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                            var purchaseNumber = $(inputs[0]).val();    //数量
                                            var useAddress = $(inputs[1]).val();        //使用地址
                                            var lastChangeTime = $(inputs[2]).val();          //上次更换时间

                                            usualData[curIndex].purchaseNumber = purchaseNumber;
                                            usualData[curIndex].useAddress = useAddress;
                                            usualData[curIndex].lastChangeTime = lastChangeTime;
                                            if (usualData.length - curIndex - 1 < 10) {
                                                var addLength = 10 - (usualData.length - curIndex - 1);
                                                for (var i = 0; i < addLength; i++) {
                                                    usualData.push({});
                                                }
                                            }
                                        });
                                        $(".erpInventoryId").each(function (index) {
                                            $(this).click(function () {
                                                var isAdd = $(this).data("add");
                                                //获取物料数据
                                                var erpInventoryList = layer.open({
                                                    type: 1,
                                                    title: '物料列表',
                                                    area: ['1100px', '750px'],
                                                    offset: '80px',
                                                    content: $('#erpInventoryListEdit').html(),
                                                    success: function () {
                                                        // 请求物料分类
                                                        let arr2tree;
                                                        admin.reqSync('enterprise/product/admin/productCategory/getProductCategoryByType', {type: 0}, function (res) {
                                                            for (let i = 0; i < res.data.length; i++) {
                                                                res.data[i].children = [];
                                                            }
                                                            arr2tree = xmSelect.arr2tree(res.data, "parentId", "id", "children", -1);
                                                        }, 'get', false);

                                                        let smlSelectInstans = xmSelect.render({
                                                            el: '#addSelfCollectedGoodsProductCategoryId',
                                                            model: {label: {type: 'text'}},
                                                            radio: true,
                                                            clickClose: true,
                                                            filterable: true,
                                                            tree: {
                                                                show: true,
                                                                strict: false,
                                                                expandedKeys: [-1],
                                                            },
                                                            tips: '请选择物料类别',
                                                            prop: {
                                                                name: 'name',
                                                                value: 'id',
                                                                children: 'children'
                                                            },
                                                            data() {
                                                                //填充数据
                                                                return arr2tree;
                                                            },
                                                            on(data) {
                                                                let parentId = data.arr.length > 0 ? data.change[0].id : "";
                                                                $("input[name='addSelfCollectedGoodsProductCategoryId']").val(parentId);
                                                            }
                                                        });

                                                        form.render("select");

                                                        var codes = "";
                                                        for (var i = 0; i < codeArr.length; i++) {
                                                            if (i == 0) {
                                                                codes = '"' + codeArr[i] + '"';
                                                            } else {
                                                                codes += "," + '"' + codeArr[i] + '"';
                                                            }
                                                        }

                                                        // 渲染表格
                                                        table.render({
                                                            elem: '#erpInventoryListEdit-table',
                                                            url: config.base + 'enterprise/product/admin/productDetail/list',
                                                            method: 'GET',
                                                            where: {
                                                                codes: codes
                                                            },
                                                            page: true,
                                                            cols: [[
                                                                {type: 'checkbox'},
                                                                {field: 'skuCode', align: 'center', title: '物料编号'},
                                                                {field: 'skuName', align: 'center', title: '物料名称'},
                                                                {
                                                                    field: 'skuSpec',
                                                                    align: 'center',
                                                                    title: '规格型号'
                                                                },
                                                                {
                                                                    field: 'productTypeName',
                                                                    align: 'center',
                                                                    title: '物料类别'
                                                                },
                                                                {
                                                                    field: 'baseUnitName',
                                                                    align: 'center',
                                                                    title: '基础单位'
                                                                },
                                                                {
                                                                    field: 'mainUnitName',
                                                                    align: 'center',
                                                                    title: '计量单位'
                                                                }
                                                            ]]
                                                        });

                                                        // 搜索按钮点击事件
                                                        $('#erpInventoryListEdit-btn-search').click(function () {
                                                            var name = $('#erpInventoryListEdit-name').val();
                                                            var code = $('#erpInventoryListEdit-code').val();
                                                            var way = $('#erpInventoryListEdit-way').val();
                                                            var categoryId = $('input[name=addSelfCollectedGoodsProductCategoryId]').val();
                                                            table.reload('erpInventoryListEdit-table', {
                                                                page: {curr: 1},
                                                                where: {
                                                                    skuCode: code,
                                                                    skuName: name,
                                                                    pricingMethod: way,
                                                                    categoryId: categoryId
                                                                }
                                                            });
                                                        });

                                                        // 确认选择
                                                        $('#erpInventoryListEdit-btn-choose').click(function () {
                                                            var checkStatus = table.checkStatus('erpInventoryListEdit-table'); //test即为基础参数id对应的值
                                                            var dataS = checkStatus.data;
                                                            console.log(dataS);
                                                            for (var i = 0; i < dataS.length; i++) {
                                                                dataS[i].skuCode = dataS[i].skuCode;
                                                                dataS[i].productName = dataS[i].skuName;
                                                                dataS[i].skuSpec = dataS[i].skuSpec;
                                                                dataS[i].mainUnitName = dataS[i].mainUnitName;
                                                                dataS[i].productTypeName = dataS[i].productTypeName;
                                                            }
                                                            if (checkStatus.data.length == 0) {
                                                                layer.msg("请选择物料", {icon: 2, time: 1000});
                                                            } else {
                                                                layer.close(erpInventoryList);
                                                                if (dataList.length >= 10) {
                                                                    dataList.splice(dataList.length - 10, 10);
                                                                }
                                                                if (!isAdd) {
                                                                    dataList.splice(index, 1);
                                                                    usualData.splice(index, 1);
                                                                    codeArr.splice(index, 1);
                                                                    for (var i = 0; i < dataS.length; i++) {
                                                                        dataList.splice(index + i, 0, dataS[i]);
                                                                        usualData.splice(index + i, 0, {})
                                                                        codeArr.splice(index + i, 0, dataS[i].skuCode);
                                                                    }
                                                                } else {
                                                                    for (var i = 0; i < dataS.length; i++) {
                                                                        dataList.push(dataS[i]);
                                                                        usualData.push({})
                                                                        codeArr.push(dataS[i].skuCode);
                                                                    }
                                                                }
                                                                //处理数据并存储
                                                                console.log(dataList);
                                                                for (var i = 0; i < dataList.length; i++) {
                                                                    if (!erpPurchasePlanInfos[i]) {
                                                                        erpPurchasePlanInfos.push({});
                                                                    }
                                                                    erpPurchasePlanInfos[i].skuCode = dataList[i].skuCode;
                                                                    erpPurchasePlanInfos[i].productName = dataList[i].productName;
                                                                    erpPurchasePlanInfos[i].skuSpec = dataList[i].skuSpec ? dataList[i].skuSpec : "";
                                                                    erpPurchasePlanInfos[i].mainUnitName = dataList[i].mainUnitName ? dataList[i].mainUnitName : "";
                                                                    erpPurchasePlanInfos[i].productTypeName = dataList[i].productTypeName ? dataList[i].productTypeName : "";
                                                                    if (usualData[i]) {
                                                                        dataList[i].purchaseNumber = usualData[i].purchaseNumber ? usualData[i].purchaseNumber : "";
                                                                        dataList[i].useAddress = usualData[i].useAddress ? usualData[i].useAddress : "";
                                                                        dataList[i].lastChangeTime = usualData[i].lastChangeTime ? usualData[i].lastChangeTime : "";
                                                                        erpPurchasePlanInfos[i].purchaseNumber = usualData[i].purchaseNumber;
                                                                        erpPurchasePlanInfos[i].useAddress = usualData[i].useAddress ? usualData[i].useAddress : "";
                                                                        erpPurchasePlanInfos[i].lastChangeTime = usualData[i].lastChangeTime ? usualData[i].lastChangeTime : "";
                                                                    } else {
                                                                        erpPurchasePlanInfos[i].purchaseNumber = "";
                                                                        erpPurchasePlanInfos[i].useAddress = "";
                                                                        erpPurchasePlanInfos[i].lastChangeTime = "";
                                                                    }
                                                                }
                                                                dataList.push({}, {}, {}, {}, {}, {}, {}, {}, {}, {});
                                                                if (usualData.length - dataList.length > 0) {
                                                                    usualData.splice(dataList.length + 1, usualData.length - dataList.length);
                                                                } else {
                                                                    var addLength = dataList.length - usualData.length;
                                                                    for (var i = 0; i < addLength; i++) {
                                                                        usualData.push({});
                                                                    }
                                                                }
                                                                // 获取数据集合并传入请购单中
                                                                table.reload('purchasePlan-edit-table', {
                                                                    data: dataList,
                                                                    limit: dataList.length
                                                                });
                                                            }
                                                        });
                                                    }
                                                })
                                            })
                                        })
                                    }
                                });

                                // 工具条点击事件
                                table.on('tool(purchasePlan-edit-table)', function (obj) {
                                    var data = obj.data;
                                    var layEvent = obj.event;
                                    if (obj.event === 'del') { //删除
                                        doDelete(obj);
                                    }
                                });

                                // 删除
                                var doDelete = function (obj) {
                                    var index = $(obj.tr).data("index");
                                    if (!$.isEmptyObject(obj.data)) {
                                        layer.confirm('确定要删除吗？', {
                                            offset: '65px',
                                            skin: 'layui-layer-admin'
                                        }, function (i) {
                                            layer.close(i);
                                            dataList.splice(index, 1);
                                            erpPurchasePlanInfos.splice(index, 1);
                                            usualData.splice(index, 1);
                                            if (codeArr.indexOf(obj.data.skuCode) >= 0) {
                                                codeArr.splice(codeArr.indexOf(obj.data.skuCode), 1);
                                            }
                                            if ($.isEmptyObject(erpPurchasePlanInfos)) {
                                                dataList = [];
                                                // 获取数据集合并传入请购单中
                                                table.reload('purchasePlan-edit-table', {
                                                    data: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
                                                    limit: 10,
                                                });
                                            } else {
                                                // 获取数据集合并传入请购单中
                                                table.reload('purchasePlan-edit-table', {
                                                    data: dataList,
                                                    limit: dataList.length
                                                });
                                            }
                                        });
                                    }
                                };

                                $("#purchasePlan-btn-aduit").click(function () {
                                    var data = $("#purchasePlan-edit").serializeObject();
                                    var purchasePlanAduit = layer.open({
                                        type: 1,
                                        title: '领料计划审核',
                                        area: ['450px', '250px'],
                                        offset: ['40%', '40%'],
                                        content: $('#purchasePlanAdit').html(),
                                        success: function () {
                                            $('#purchasePlan-reason').val(reason);
                                            var data = $("#purchasePlan-edit").serializeObject();
                                            $("#purchasePlan-aduit-submit1").click(function () {
                                                layer.close(purchasePlanAduit);
                                                commit('fail', data);
                                            })

                                            $("#purchasePlan-aduit-submit2").click(function () {
                                                layer.close(purchasePlanAduit);
                                                commit('success', data);
                                            })
                                        }
                                    })
                                    return false;
                                })

                                function commit(status, data) {
                                    if (erpPurchasePlanInfos.length > 0) {
                                        data.planInfoList = erpPurchasePlanInfos;
                                        data.status = status;
                                        data.reason = $('#purchasePlan-reason').val();
                                        layer.load(2);
                                        admin.req('enterprise/purchase/admin/purchasePlan/audit', JSON.stringify(data), function (data) {
                                            layer.closeAll('loading');
                                            if (data.code == 200) {
                                                layer.msg(data.msg, {icon: 1, time: 500});
                                                table.reload('purchasePlanTable');
                                                layer.closeAll('page');
                                            } else {
                                                layer.msg(data.msg, {icon: 2, time: 1000});
                                            }
                                        }, 'POST', false, "application/json");
                                    } else {
                                        layer.msg("未输入发生数据，无法保存", {icon: 2, time: 1000});
                                    }
                                    return false;
                                }

                            } else {
                                layer.msg(data.msg, {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    } else {
                        // 渲染表格
                        table.render({
                            elem: '#purchasePlan-edit-table',
                            data: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
                            limit: 10,
                            cellMinWidth: 100,
                            colGroup: true,
                            // height: 450,
                            cols: [[
                                {type: 'numbers'},
                                {
                                    field: 'productName',
                                    align: 'center',
                                    title: '物料名称',
                                    templet: '<div><span class="erpInventoryId" style="display: block;cursor: pointer;" data-add="{{d.productName != null ? false : true}}">{{d.productName != null ? d.productName : "请选择物料"}}</span></div>'
                                },
                                {field: 'skuCode', align: 'center', title: '物料编号'},
                                {field: 'skuSpec', align: 'center', title: '规格型号'},
                                {field: 'mainUnitName', align: 'center', title: '计量单位'},
                                {field: 'productTypeName', align: 'center', title: '物料类别'},
                                {
                                    title: "数量<span style='color:red;'>*</span>",
                                    align: 'center',
                                    templet: '#purchaseNumberEdit'
                                },
                                {title: '使用地址', align: 'center', templet: '#useAddressEdit'},
                                {title: '上次更新时间', align: 'center', templet: '#lastChangeTimeEdit'},
                                {align: 'center', toolbar: '#purchasePlan-edit-table-bar', title: '操作', width: 100}
                            ]],
                            done: function () {
                                $("input[name='purchaseNumber']").on("input", function (e) {
                                    //获取input输入的值
                                    var curValue = e.delegateTarget.value;
                                    var curIndex = $(this).closest("tr").data("index");
                                    if (erpPurchasePlanInfos[curIndex]) {
                                        erpPurchasePlanInfos[curIndex].purchaseNumber = curValue;
                                    }
                                    var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                    var purchaseNumber = $(inputs[0]).val();    //数量
                                    var useAddress = $(inputs[1]).val();        //使用地址
                                    var lastChangeTime = $(inputs[2]).val();          //上次更换时间
                                    if (purchaseNumber == '' || purchaseNumber < 0) {
                                        purchaseNumber = 0;
                                        $(inputs[0]).val(0);
                                    }
                                    usualData[curIndex].purchaseNumber = purchaseNumber;
                                    usualData[curIndex].useAddress = useAddress;
                                    usualData[curIndex].lastChangeTime = lastChangeTime;
                                    if (usualData.length - curIndex - 1 < 10) {
                                        var addLength = 10 - (usualData.length - curIndex - 1);
                                        for (var i = 0; i < addLength; i++) {
                                            usualData.push({});
                                        }
                                    }
                                });
                                $("input[name='useAddress']").on("input", function (e) {
                                    //获取input输入的值
                                    var curValue = e.delegateTarget.value;
                                    var curIndex = $(this).closest("tr").data("index");
                                    if (erpPurchasePlanInfos[curIndex]) {
                                        erpPurchasePlanInfos[curIndex].useAddress = curValue;
                                    }
                                    var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                    var purchaseNumber = $(inputs[0]).val();    //数量
                                    var useAddress = $(inputs[1]).val();        //使用地址
                                    var lastChangeTime = $(inputs[2]).val();          //上次更换时间

                                    usualData[curIndex].purchaseNumber = purchaseNumber;
                                    dataList[curIndex].useAddress = useAddress;
                                    usualData[curIndex].useAddress = useAddress;
                                    usualData[curIndex].lastChangeTime = lastChangeTime;
                                    if (usualData.length - curIndex - 1 < 10) {
                                        var addLength = 10 - (usualData.length - curIndex - 1);
                                        for (var i = 0; i < addLength; i++) {
                                            usualData.push({});
                                        }
                                    }
                                });
                                $("input[name='lastChangeTime']").on("input", function (e) {
                                    //获取input输入的值
                                    var curValue = e.delegateTarget.value;
                                    var curIndex = $(this).closest("tr").data("index");
                                    if (erpPurchasePlanInfos[curIndex]) {
                                        erpPurchasePlanInfos[curIndex].lastChangeTime = curValue;
                                    }
                                    var inputs = $(this).closest("tr").find("input");//0 数量 2使用地址 3上次更新时间

                                    var purchaseNumber = $(inputs[0]).val();    //数量
                                    var useAddress = $(inputs[1]).val();        //使用地址
                                    var lastChangeTime = $(inputs[2]).val();          //上次更换时间

                                    usualData[curIndex].purchaseNumber = purchaseNumber;
                                    usualData[curIndex].useAddress = useAddress;
                                    dataList[curIndex].lastChangeTime = lastChangeTime;
                                    usualData[curIndex].lastChangeTime = lastChangeTime;
                                    if (usualData.length - curIndex - 1 < 10) {
                                        var addLength = 10 - (usualData.length - curIndex - 1);
                                        for (var i = 0; i < addLength; i++) {
                                            usualData.push({});
                                        }
                                    }
                                });

                                $(".erpInventoryId").each(function (index) {
                                    $(this).click(function () {
                                        var isAdd = $(this).data("add");
                                        //获取物料数据
                                        var erpInventoryList = layer.open({
                                            type: 1,
                                            title: '物料SKU列表',
                                            area: ['1100px', '750px'],
                                            offset: '80px',
                                            content: $('#erpInventoryListEdit').html(),
                                            success: function () {
                                                // 请求物料分类
                                                admin.initDataSelectTree(true, 'enterprise/product/admin/productType/getTreeData', null, 'selectProductType', null, "物料类别", "addSelfCollectedGoodsProductCategoryId", null);

                                                form.render("select");

                                                var codes = "";
                                                for (var i = 0; i < codeArr.length; i++) {
                                                    if (i == 0) {
                                                        codes = '"' + codeArr[i] + '"';
                                                    } else {
                                                        codes += "," + '"' + codeArr[i] + '"';
                                                    }
                                                }

                                                // 渲染表格
                                                table.render({
                                                    elem: '#erpInventoryListEdit-table',
                                                    url: config.base + 'enterprise/product/admin/productDetail/list',
                                                    method: 'GET',
                                                    where: {
                                                        codes: codes
                                                    },
                                                    page: true,
                                                    cols: [[
                                                        {type: 'checkbox'},
                                                        {field: 'skuCode', align: 'center', title: '物料编号'},
                                                        {field: 'skuName', align: 'center', title: '物料名称'},
                                                        {
                                                            field: 'skuSpec',
                                                            align: 'center',
                                                            title: '规格型号'
                                                        },
                                                        {field: 'productTypeName', align: 'center', title: '物料类别'},
                                                        {
                                                            field: 'baseUnitName',
                                                            align: 'center',
                                                            title: '基础单位'
                                                        },
                                                        {field: 'mainUnitName', align: 'center', title: '计量单位'}
                                                    ]]
                                                });

                                                // 搜索按钮点击事件
                                                $('#erpInventoryListEdit-btn-search').click(function () {
                                                    var name = $('#erpInventoryListEdit-name').val();
                                                    var code = $('#erpInventoryListEdit-code').val();
                                                    var way = $('#erpInventoryListEdit-way').val();
                                                    var categoryId = $('input[name=addSelfCollectedGoodsProductCategoryId]').val();
                                                    table.reload('erpInventoryListEdit-table', {
                                                        page: {curr: 1},
                                                        where: {
                                                            skuCode: code,
                                                            skuName: name,
                                                            pricingMethod: way,
                                                            categoryId: categoryId
                                                        }
                                                    });
                                                });

                                                // 确认选择
                                                $('#erpInventoryListEdit-btn-choose').click(function () {
                                                    var checkStatus = table.checkStatus('erpInventoryListEdit-table'); //test即为基础参数id对应的值
                                                    var dataS = checkStatus.data;
                                                    for (var i = 0; i < dataS.length; i++) {
                                                        dataS[i].skuCode = dataS[i].skuCode;
                                                        dataS[i].productName = dataS[i].skuName;
                                                        dataS[i].skuSpec = dataS[i].skuSpec;
                                                        dataS[i].mainUnitName = dataS[i].mainUnitName;
                                                        dataS[i].productTypeName = dataS[i].productTypeName;
                                                    }
                                                    if (checkStatus.data.length == 0) {
                                                        layer.msg("请选择物料", {icon: 2, time: 1000});
                                                    } else {
                                                        layer.close(erpInventoryList);
                                                        if (dataList.length >= 10) {
                                                            dataList.splice(dataList.length - 10, 10);
                                                        }
                                                        if (!isAdd) {
                                                            dataList.splice(index, 1);
                                                            usualData.splice(index, 1);
                                                            codeArr.splice(index, 1);
                                                            for (var i = 0; i < dataS.length; i++) {
                                                                dataList.splice(index + i, 0, dataS[i]);
                                                                usualData.splice(index + i, 0, {})
                                                                codeArr.splice(index + i, 0, dataS[i].skuCode);
                                                            }
                                                        } else {
                                                            for (var i = 0; i < dataS.length; i++) {
                                                                dataList.push(dataS[i]);
                                                                usualData.push({})
                                                                codeArr.push(dataS[i].skuCode);
                                                            }
                                                        }
                                                        //处理数据并存储
                                                        for (var i = 0; i < dataList.length; i++) {
                                                            if (!erpPurchasePlanInfos[i]) {
                                                                erpPurchasePlanInfos.push({});
                                                            }
                                                            erpPurchasePlanInfos[i].skuCode = dataList[i].skuCode;
                                                            erpPurchasePlanInfos[i].productName = dataList[i].productName;
                                                            erpPurchasePlanInfos[i].skuSpec = dataList[i].skuSpec ? dataList[i].skuSpec : "";
                                                            erpPurchasePlanInfos[i].mainUnitName = dataList[i].mainUnitName ? dataList[i].mainUnitName : "";
                                                            erpPurchasePlanInfos[i].productTypeName = dataList[i].productTypeName ? dataList[i].productTypeName : "";
                                                            if (usualData[i]) {
                                                                dataList[i].purchaseNumber = usualData[i].purchaseNumber ? usualData[i].purchaseNumber : "";
                                                                dataList[i].useAddress = usualData[i].useAddress ? usualData[i].useAddress : "";
                                                                dataList[i].lastChangeTime = usualData[i].lastChangeTime ? usualData[i].lastChangeTime : "";
                                                                erpPurchasePlanInfos[i].purchaseNumber = usualData[i].purchaseNumber ? usualData[i].purchaseNumber : "";
                                                                erpPurchasePlanInfos[i].useAddress = usualData[i].useAddress ? usualData[i].useAddress : "";
                                                                erpPurchasePlanInfos[i].lastChangeTime = usualData[i].lastChangeTime ? usualData[i].lastChangeTime : ""
                                                            } else {
                                                                erpPurchasePlanInfos[i].purchaseNumber = "";
                                                                erpPurchasePlanInfos[i].useAddress = "";
                                                                erpPurchasePlanInfos[i].lastChangeTime = "";
                                                            }
                                                        }
                                                        dataList.push({}, {}, {}, {}, {}, {}, {}, {}, {}, {});
                                                        if (usualData.length - dataList.length > 0) {
                                                            usualData.splice(dataList.length + 1, usualData.length - dataList.length);
                                                        } else {
                                                            var addLength = dataList.length - usualData.length;
                                                            for (var i = 0; i < addLength; i++) {
                                                                usualData.push({});
                                                            }
                                                        }
                                                        // 获取数据集合并传入请购单中
                                                        table.reload('purchasePlan-edit-table', {
                                                            data: dataList,
                                                            limit: dataList.length
                                                        });
                                                    }
                                                });
                                            }
                                        })
                                    })
                                })
                            }
                        });


                        // 工具条点击事件
                        table.on('tool(purchasePlan-edit-table)', function (obj) {
                            var data = obj.data;
                            var layEvent = obj.event;
                            if (obj.event === 'del') { //删除
                                doDelete(obj);
                            }
                        });

                        // 删除
                        var doDelete = function (obj) {
                            var index = $(obj.tr).data("index");
                            if (!$.isEmptyObject(obj.data)) {
                                layer.confirm('确定要删除吗？', {
                                    offset: '65px',
                                    skin: 'layui-layer-admin'
                                }, function (i) {
                                    layer.close(i);
                                    //obj.del();
                                    dataList.splice(index, 1);
                                    erpPurchasePlanInfos.splice(index, 1);
                                    usualData.splice(index, 1);
                                    if (codeArr.indexOf(obj.data.skuCode) >= 0) {
                                        codeArr.splice(codeArr.indexOf(obj.data.skuCode), 1);
                                    }
                                    if ($.isEmptyObject(dataList[0])) {
                                        dataList = [];
                                        // 获取数据集合并传入请购单中
                                        table.reload('purchasePlan-edit-table', {
                                            data: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
                                            limit: 10,
                                        });
                                    } else {
                                        // 获取数据集合并传入请购单中
                                        table.reload('purchasePlan-edit-table', {
                                            data: dataList,
                                            limit: dataList.length
                                        });
                                    }
                                });
                            }
                        };
                    }

                    form.val('purchasePlan-edit', data);


                    // 表单提交事件
                    form.on('submit(purchasePlan-edit-submit)', function (d) {
                        if (erpPurchasePlanInfos.length > 0) {
                            d.field.planInfoList = erpPurchasePlanInfos;
                            layer.load(2);
                            admin.req(data ? 'enterprise/purchase/admin/purchasePlan/update' : 'enterprise/purchase/admin/purchasePlan/add', JSON.stringify(d.field), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('purchasePlanTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2, time: 1000});
                                }
                            }, data ? 'PUT' : 'POST', false, "application/json");
                        } else {
                            layer.msg("未输入发生数据，无法保存", {icon: 2, time: 1000});
                        }
                        return false;
                    });
                    $.fn.serializeObject = function () {
                        var ct = this.serializeArray();
                        var obj = {};
                        $.each(ct, function () {
                            if (obj[this.name] !== undefined) {
                                if (!obj[this.name].push) {
                                    obj[this.name] = [obj[this.name]];
                                }
                                obj[this.name].push(this.value || "");
                            } else {
                                obj[this.name] = this.value || "";
                            }
                        });
                        return obj;
                    };

                }
            });
        }

        var showPlanInfo = function (obj) {
            var dataList = obj.data;
            var purchasePlanAduit = admin.open({
                type: 1,
                title: '领料计划详情',
                area: ['1200px', '80%'],
                content: $('#purchasePlanInfo').html(),
                success: function () {
                    table.render({
                        elem: '#purchasePlanInfo-table',
                        url: config.base + 'enterprise/purchase/admin/purchasePlanDetail/list',
                        method: 'GET',
                        where: {
                            planId: dataList.id,
                            order: "asc"
                        },
                        page: true,
                        cols: [[
                            {type: 'numbers'},
                            {field: 'productName', align: 'center', title: '物料名称'},
                            {field: 'skuCode', align: 'center', title: '物料编号'},
                            {field: 'skuSpec', align: 'center', title: '规格型号'},
                            {field: 'mainUnitName', title: '计量单位', align: 'center'},
                            {field: 'productTypeName', align: 'center', title: '物料类别'},
                            {field: 'purchaseNumber', title: '领料数量', align: 'center'},
                            {field: 'useAddress', title: '使用地址', align: 'center'},
                            {field: 'lastChangeTime', title: '上次更换时间', align: 'center'},
                        ]]
                    });
                    // 获取部门
                    $.ajax({
                        type: 'GET',
                        url: config.base + 'user/admin/department/list',
                        data: {limit: 99999},
                        dataType: "json",
                        contentType: "application/json",
                        beforeSend: function (xhr) {
                            var token = config.getToken();
                            if (token) {
                                xhr.setRequestHeader('Authorization', 'bearer ' + token);
                            }
                        },
                        success: function (data) {
                            if (200 == data.code) {
                                laytpl(purchasePlanDepartmentList.innerHTML).render(data.data, function (html) {
                                    $("form select[name='departmentId']").html(html);
                                    form.render('select');
                                    form.val("purchasePlan-form", dataList);
                                });
                            } else {
                                layer.msg('获取部门失败', {icon: 2, time: 1000});
                            }
                        }
                    })

                    // 获取紧急程度
                    admin.req('enterprise/purchase/admin/purchaseUrgency/list', {limit: 9999}, function (res) {
                        if (res.code == 200) {
                            var html = '<option value="">请选择紧急程度</option>';
                            for (var k = 0; k < res.data.length; k++) {
                                html += '<option value="' + res.data[k].id + '">' + res.data[k].purchaseUrgencyName + '</option>';
                            }
                            $("form select[name='purchaseUrgencyId']").html(html);
                            form.render('select');
                            if (dataList && dataList.purchaseUrgencyId) {
                                form.val('purchasePlan-form', dataList);
                            }
                        } else {
                            layer.msg('获取紧急程度失败', {icon: 2, time: 1000});
                        }
                    }, 'GET');


                }
            })
            form.val("purchasePlan-form", dataList);
        }

        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchasePlan/' + obj.data.id, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 1000});
                    }
                }, 'DELETE');
            });
        };

        // 导出excel
        $('#purchasePlanBtnExport').click(function () {
            var checkRows = table.checkStatus('purchasePlanTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>